﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web.UI.DataVisualization.Charting;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //饼图上的一个点击事件，用来超链接
        Chart2.Click += new ImageMapEventHandler(Chart2_Click);
        //圆饼百分比
        Chart2.Series["Series1"].Label = "#PERCENT{P2}";
        //图列文字
        Chart2.Series["Series1"].LegendText = "#VALX";
        //返回X轴上的一个值
        Chart2.Series[0].PostBackValue = "#VALX"; 
      //  Chart2.Series["Series1"].LegendUrl = "http://www.baidu.com";
        //鼠标放上去显示本区域有多少企业
        Chart2.Series[0].ToolTip = "#AXISLABEL #VALY";

        BindChart();
        PieChart1();
    }

    #region  饼状图
    void Chart2_Click(object sender, ImageMapEventArgs e)
    {
        //Response.Write(e.PostBackValue);
        //if (e.PostBackValue=="化学工业")
        //{
        //    Response.Redirect("http://www.cwdws.com");
        //    Response.End();
        //}
        switch (e.PostBackValue)
        {
            case "化学工业":
            Response.Redirect("http://www.cwdws.com");
            Response.End();
            break;
            case "非煤矿山":
            Response.Redirect("http://www.baidu.com");
            Response.End();
            break;
            case "烟花爆竹":
            Response.Redirect("http://www.google.com.hk/webhp?hl=zh-CN&sourceid=cnhp");
            Response.End();
            break;
            case "其他":
            Response.Redirect("http://www.cwdws.com");
            Response.End();
            break;

        };

    }
    private void PieChart1()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
        SqlDataReader dr;
        Title t2 = new Title("企业比例图");
        Chart2.Titles.Add(t2);
        //化学工业企业总和
        int sumWhp = 0;
        //非煤矿山企业总和
        int sumFeimei = 0;
        //烟花爆竹企业总和
        int sumBaozhu = 0;
        //其他企业总和
        int sumOther = 0;
        int cityID = -1;
        try
        {
            conn.Open();        
            #region 全市企业
            if (cityID==-1)
            {
          //计算化学工业企业总和
            SqlCommand cmd2 = new SqlCommand();
            cmd2.Connection = conn;
            cmd2.CommandType = CommandType.Text;
            cmd2.CommandText = @"select COUNT(mc) as hxqy from Company as c join comp_fl as fl
                                  on c.Grade =fl.bm  and bm2=0";
            dr = cmd2.ExecuteReader();
            while(dr.Read())
            {
                sumWhp = Convert.ToInt32(dr["hxqy"].ToString());
            }
            dr.Close ();
          
         

            //烟花爆竹企业总和
            SqlCommand cmd3 = new SqlCommand();
            cmd3.Connection = conn;
            cmd3.CommandType = CommandType.Text;
            cmd3.CommandText = @"select COUNT(mc) as yhbz from Company as c join comp_fl as fl
                                  on c.Grade =fl.bm  and bm2=7";

            dr = cmd3.ExecuteReader();
            while (dr.Read())
            {
                sumBaozhu = Convert.ToInt32(dr["yhbz"].ToString());
            }
            dr.Close();

            //非煤矿山企业总和
            SqlCommand cmd4 = new SqlCommand();
            cmd4.Connection = conn;
            cmd4.CommandType = CommandType.Text;
            cmd4.CommandText = @"select COUNT(mc) as fmks from Company as c join comp_fl as fl
                                  on c.Grade =fl.bm  and bm2=6";

            dr = cmd4.ExecuteReader();
            while (dr.Read())
            {
                sumFeimei = Convert.ToInt32(dr["fmks"].ToString());
            }
            dr.Close();

            //其它企业总和
            SqlCommand cmd5 = new SqlCommand();
            cmd5.Connection = conn;
            cmd5.CommandType = CommandType.Text;
            cmd5.CommandText = @"select COUNT(mc) as qt from Company as c join comp_fl as fl
                                  on c.Grade =fl.bm  and bm2=8";
            dr = cmd5.ExecuteReader();
            while (dr.Read())
            {
                sumOther = Convert.ToInt32(dr["qt"].ToString());
            }
            dr.Close();
            }
            #endregion

            #region 按区域查询
            else 
            {
                 //计算化学工业企业总和
            SqlCommand cmd2 = new SqlCommand();
            cmd2.Connection = conn;
            cmd2.CommandType = CommandType.Text;
            cmd2.CommandText = @"select COUNT(mc) as hxqy from Company as c join comp_fl as fl
                                  on c.Grade =fl.bm  and bm2=0 and OrgId='"+cityID+"'";
            dr = cmd2.ExecuteReader();
            while(dr.Read())
            {
                sumWhp = Convert.ToInt32(dr["hxqy"].ToString());
            }
            dr.Close ();
          
         

            //烟花爆竹企业总和
            SqlCommand cmd3 = new SqlCommand();
            cmd3.Connection = conn;
            cmd3.CommandType = CommandType.Text;
            cmd3.CommandText = @"select COUNT(mc) as yhbz from Company as c join comp_fl as fl
                                  on c.Grade =fl.bm  and bm2=7 and OrgId='"+cityID+"'";

            dr = cmd3.ExecuteReader();
            while (dr.Read())
            {
                sumBaozhu = Convert.ToInt32(dr["yhbz"].ToString());
            }
            dr.Close();

            //非煤矿山企业总和
            SqlCommand cmd4 = new SqlCommand();
            cmd4.Connection = conn;
            cmd4.CommandType = CommandType.Text;
            cmd4.CommandText = @"select COUNT(mc) as fmks from Company as c join comp_fl as fl
                                  on c.Grade =fl.bm  and bm2=6 and OrgId='"+cityID+"'";

            dr = cmd4.ExecuteReader();
            while (dr.Read())
            {
                sumFeimei = Convert.ToInt32(dr["fmks"].ToString());
            }
            dr.Close();

            //其它企业总和
            SqlCommand cmd5 = new SqlCommand();
            cmd5.Connection = conn;
            cmd5.CommandType = CommandType.Text;
            cmd5.CommandText = @"select COUNT(mc) as qt from Company as c join comp_fl as fl
                                  on c.Grade =fl.bm  and bm2=8 and OrgId='"+cityID+"'";
            dr = cmd5.ExecuteReader();
            while (dr.Read())
            {
                sumOther = Convert.ToInt32(dr["qt"].ToString());
            }
            dr.Close();
            }
            #endregion

            double[] yValues = { sumWhp, sumFeimei, sumBaozhu, sumOther };
            string[] xValues = { "化学工业", "非煤矿山", "烟花爆竹", "其他" };
            Chart2.Series["Series1"].Points.DataBindXY(xValues, yValues);  
        }

        catch (Exception ex)
        {
            ex.Message.ToString();
        }
        finally
        {
            conn.Close();
        }

    }
    #endregion

    #region 柱状图
    private DataTable BindChart()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
        SqlDataReader dr;
        //中间X,Y线条的颜色设置
        Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.Yellow;
        Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Blue;
        //大标题
        Title t2 = new Title("区域合格企业");
        Chart1.Titles.Add(t2);

        //背景色设置
        Chart1.ChartAreas["ChartArea1"].ShadowColor = Color.Transparent;
        Chart1.ChartAreas["ChartArea1"].BackColor = Color.Azure;
        Chart1.ChartAreas["ChartArea1"].BackGradientStyle = GradientStyle.TopBottom;
        Chart1.ChartAreas["ChartArea1"].BackSecondaryColor = Color.White;
       
     
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            //cmd.CommandText = @"select OrgId,OrgName from Organize";
            cmd.CommandText = @"
select o.orgname,c.compname,count(c.CompNo)as dd from (((company as c 
inner join report_month_yhpc as hx
on c.compNo=hx.compNO and hx.tijiao=1)
inner join comp_yjyuan as ya on c.compNo=ya.compNo and ya.flag=2)
INNER jOin xkz_yanhua as xkz on c.compNo=xkz.name)
Inner join organize as o on c.orgid=o.orgid group by c.compname,o.orgname";

            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            //Chart1.Series["Series1"].XValueMember = "orgname";
            //Chart1.Series["Series1"].YValueMembers = "dd";
            Chart1.Series["Series1"].Points.DataBindXY(dr, "orgname", dr, "dd");
            // Chart1.Series["Series2"].Points.DataBindXY(dr, "OrgName", dr, "ff");
            dr.Close();


            SqlCommand cmd1 = new SqlCommand();
            cmd1.Connection = conn;
            cmd1.CommandType = CommandType.Text;
            //cmd.CommandText = @"select OrgId,OrgName from Organize";
            cmd1.CommandText = @"
select o.orgname,c.compname,count(c.CompNo)as dd from (((company as c 
inner join report_month_yhpc as hx
on c.compNo=hx.compNO and hx.tijiao<>1)
inner join comp_yjyuan as ya on c.compNo=ya.compNo and ya.flag<>2)
INNER jOin xkz_yanhua as xkz on c.compNo=xkz.name)
Inner join organize as o on c.orgid=o.orgid group by c.compname,o.orgname";

            dr = cmd1.ExecuteReader(CommandBehavior.CloseConnection);
            Chart1.Series["Series2"].Points.DataBindXY(dr, "orgname", dr, "dd");
            dr.Close();
          
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }

        finally
        {
            conn.Close();
        }
        return null;
    }
    #endregion

}
